<?php


namespace RedMVC\Auth\Adapter;
use RedMVC;

/**
 * $memory is an array like:
 * array(
 *  array(
 *      'login' => 'test',
 *      'pwd' => 'fnskdjnfk' 
 *  ),
 * )
 *
 */   

/**
 * Memory class
 *  
 * @category    RedMVC
 * @package     Auth
 * @subpackage  Adapter 
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Memory implements IAdapter{

    protected $_memory;
    protected $_login;
    protected $_pwd;

    public function __construct(array $memory, $login, $pwd){
        $this->_memory = $memory;
        $this->_login = $login;
        $this->_pwd = $pwd;
    }
    
    protected function _findLogin($login, $pwd){
        foreach($this->_memory as $possibleLogin){
            if($possibleLogin['login'] == $login && $possibleLogin['pwd'] == $pwd){
                return $possibleLogin;
            }
        }
        return null;        
    }

    public function login(){
        $login = $this->_findLogin($this->_login, $this->_pwd);
        if(null !== $login){
            $this->_loginResult = $login;
            return true;
        }
        return false;
    }
    
    public function getLoginResult(){
        return $this->_loginResult;
    }

}